// getAt  从文本中提取@的人员  替换显示效果
Handlebars.registerHelper('getAt', function (txt, atWhoData, canAt, ats) {
    if (!txt) {
        return '';
    }

    // 有@权限的才替换
    // if (canAt) {
    //     txt = txt.replace(/@all\s/g, '<span class="topic-item-at">$1</span>');
    // }

    if (ats.length) {
        txt = txt.replace(/(@[^@]+?\s)/g, function($, $1) {
            var name = $1.slice(1, $1.length - 1);

            // 匹配到姓名的才替换
            if (ats.some(function(item) {
                return item === name;
            })) {
                return '<span class="topic-item-at">' + $1 + '</span>';
            } else {
                return $1;
            }
        })
    }

    return txt.replace(/\r\n|\r|\n/g, '<br>');
})
Handlebars.registerHelper('generateURL', function(url) {
    if (!url) {
        return '';
    }

    if (url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1 || url.indexOf('//') !== -1) {
        return url;
    }

    return '//' + url;

    // var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;

    // return url.replace(reg, '$1$2');
});
Handlebars.registerHelper('payView', function(pay) {
    if (!pay) {
        return '';
    }

    pay = pay.split('|');

    if (!pay[1]) {
        return '';
    }

    if (pay[0] === 'month') {
        return '月薪 ' + (pay[1] || '') + ' 元';
    } else if (pay[0] === 'year') {
        return '年薪 ' + (pay[1] || '') + ' 万元';
    }
});

Handlebars.registerHelper('select', function(selected, options) {
    return options.fn(this).replace(
        new RegExp(' value=\"' + selected + '\"'),
        '$& selected="selected"');
});

Handlebars.registerHelper('toJSONString', function (json) {
    if (typeof json === 'string') {
        return json;
    }

    return JSON.stringify(json);
});
// avatar  本地替换OM头像路径
Handlebars.registerHelper('avatar', function (src) {
    return src;

    // if (!src) {
    //     return '';
    // }

    // var avatarHost = 'in2.4399om.com';

    // return src.replace(/(new|local\.|test\.4399|branch\.4399)om\.com/g, avatarHost)
    //     .replace('om.116dev.com', avatarHost);
})
// equal
Handlebars.registerHelper('equal', function (v1, v2, options) {
    if (v1 == v2) {
        return options.fn(this)
    } else {
        return options.inverse(this)
    }
})

// less
Handlebars.registerHelper('less', function (v1, v2, options) {
    if (v1 < v2) {
        return options.fn(this)
    } else {
        return options.inverse(this)
    }
})

// slice
Handlebars.registerHelper('slice', function (string, start, end) {
    var result = ''
    if(typeof end != 'object'){
        result = string.slice(parseInt(start), parseInt(end))
    }else{
        result = string.slice(0, parseInt(start))
    }
    return result
})

// slice
Handlebars.registerHelper('key_value', function (obj, options) {
    var result = []
    for(var key in obj){
        result.push(options.fn({
            '$key': key,
            '$value': obj[key]
        }))
    }
    return result.join('')
})

Handlebars.registerHelper('add', function (v1, v2) {
    return Number(v1) + Number(v2);
});

Handlebars.registerHelper('value', function (obj, key) {
    return obj[key] || ''
})

// array
Handlebars.registerHelper('array', function (arr, index) {
    return arr[index]
})

// neq
Handlebars.registerHelper('neq', function (v1, v2, options) {
    if (v1 != v2) {
        return options.fn(this)
    } else {
        return options.inverse(this)
    }
})

Handlebars.registerHelper('html', function(text) {
    return new Handlebars.SafeString(text)
})

Handlebars.registerHelper('date_format', function(text, format) {
    var date = new Date(text == parseInt(text) ? parseInt(text) * 1000 : text)

    var data = {
        year: date.getFullYear(),
        year_short: date.getYear(),
        month: date.getMonth() + 1,
        day: date.getDate(),
        hour: date.getHours(),
        minute: date.getMinutes(),
        second: date.getSeconds()
    }

    var addZero = function(num){
        num = parseInt(num)
        return num > 9 ? num : '0' + num
    }

    format = typeof format == 'object' ? 'yyyy-mm-dd' : format
    format = format
        .replace(/yyyy/g, data['year'])
        .replace(/yy/g, data['year_short'])

        .replace(/mm/g, addZero(data['month']))
        .replace(/m/g, data['month'])

        .replace(/dd/g, addZero(data['day']))
        .replace(/d/g, data['day'])

        .replace(/hh/g, addZero(data['hour']))
        .replace(/h/g, data['hour'])

        .replace(/ii/g, addZero(data['minute']))
        .replace(/i/g, data['minute'])

        .replace(/ss/g, addZero(data['second']))
        .replace(/s/g, data['second'])

    return format
})

Handlebars.registerHelper('breaklines', function(text) {
    text = Handlebars.Utils.escapeExpression(text)
    text = text.toString().replace(/(\r\n|\n|\r)/gm, '<br>')

    return new Handlebars.SafeString(text)
})

Handlebars.registerHelper('substr', function(text, len, ex){
    ex = typeof ex == 'object' ? '' : ex
    ex = ex === false ? '' : (ex || '...')
    text = text.substr(0, len) + ex
    return text
})

Handlebars.registerHelper('or', function (v1, v2, options) {
    if (v1 || v2) {
        return options.fn(this)
    } else {
        return options.inverse(this)
    }
})

Handlebars.registerHelper('ifIn', function (elem, list, options) {
    if (!list) {
        return;
    }

    if (list.includes(elem)) {
        return options.fn(this);
    }
    return options.inverse(this);
});